package com.coocaa.ops.admin.v1.tab.provider.impl;

import com.coocaa.ops.admin.base.AbstractBizImpl;
import com.coocaa.ops.admin.base.exception.AdminBizException;
import com.coocaa.ops.admin.base.language.CommonLanguageProperties;
import com.coocaa.ops.admin.base.service.AbstractSaveServiceImpl;
import com.coocaa.ops.admin.base.validator.InputParamsValidator;
import com.coocaa.ops.admin.v1.audit.AuditUtil;
import com.coocaa.ops.admin.v1.audit.provider.BaseAuditTaskProviderImpl;
import com.coocaa.ops.admin.v1.dmp.util.DmpRegistryRlsUtil;
import com.coocaa.ops.admin.v1.tab.biz.impl.TabInfoBizImpl;
import com.coocaa.ops.admin.v1.tab.biz.impl.TabPanelRlsBizImpl;
import com.coocaa.ops.admin.v1.tab.provider.ITabInfoRlsUpdateProvider;
import com.coocaa.ops.admin.v1.tab.provider.ITabInfoUpdateProvider;
import com.coocaa.ops.common.core.base.enums.ResourceStatusEnums;
import com.coocaa.ops.common.core.base.enums.ResourceTypeEnum;
import com.coocaa.ops.common.core.base.model.R;
import com.coocaa.ops.common.core.base.util.ResourceStatusValidator;
import com.coocaa.ops.common.core.biz.audit.entity.AuditTaskInfoEntity;
import com.coocaa.ops.common.core.biz.tab.constant.TabTypeConstants;
import com.coocaa.ops.common.core.biz.tab.entity.TabInfoEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

/**
 * @author bjh
 * @description 版面信息原始表-更新provider接口实现
 * @date : 2020-06-15 16:27:18
 */
@Service
public class TabInfoUpdateProviderImpl extends AbstractSaveServiceImpl<TabInfoEntity> implements ITabInfoUpdateProvider {
    @Autowired
    private TabInfoBizImpl thisBizImpl;
    @Autowired
    private ITabInfoRlsUpdateProvider thisRlsUpdateProvider;
    @Autowired
    private TabPanelRlsBizImpl tabPanelRlsBiz;

    private static CommonLanguageProperties commonLanguageProperties;

    @Override
    public AbstractBizImpl getAbstractBizImpl() {
        return thisBizImpl;
    }

    @Autowired
    public void setCommonLanguageProperties(CommonLanguageProperties commonLanguageProperties) {
        TabInfoUpdateProviderImpl.commonLanguageProperties = commonLanguageProperties;
    }

    @Override
    public void handleSaveRlsInfo(TabInfoEntity inputEntity, Integer userId) {
        thisRlsUpdateProvider.saveRls(inputEntity, userId);
    }

    /**
     * 保存
     *
     * @param inputEntity
     * @param userId
     * @return
     */
    @Override
    public TabInfoEntity save(TabInfoEntity inputEntity, Integer userId) {
        this.saveBaseInfo(inputEntity.getId(), inputEntity, userId);
        return inputEntity;
    }


    /**
     * 批量删除
     *
     * @param ids
     * @param userId
     * @return
     */
    @Override
    public String deleteBatch(Integer[] ids, Integer userId) {
        for (Integer id : ids) {
            TabInfoEntity entity = (TabInfoEntity) thisBizImpl.queryById(id);
            if (ResourceStatusValidator.isOnline(entity.getStatus())) {
                return "[ID：" + entity.getId() + "][" + commonLanguageProperties.getName() + "：" + entity.getTabName() + "]" + commonLanguageProperties.getAuditSuccess() + commonLanguageProperties.getNotDelete();
            }
            if (thisBizImpl.isTabHasUsed(id)) {
                return "[ID：" + entity.getId() + "][" + commonLanguageProperties.getName() + "：" + entity.getTabName() + "]" + commonLanguageProperties.getInUse() + commonLanguageProperties.getNotDelete();
            }
            //判断板块是否已提交流程
            AuditTaskInfoEntity taskInfo = AuditUtil.getTaskByIdAndVersionAndType(entity.getId(), entity.getCurrentVersion(), ResourceTypeEnum.TAB.getValue());
            if (taskInfo != null && taskInfo.getTaskFlag() == 1) {
                return "[ID：" + entity.getId() + "][" + commonLanguageProperties.getName() + "：" + entity.getTabName() + "]" + commonLanguageProperties.getSubmitReview() + commonLanguageProperties.getNotDelete();
            }
            // 是否存在副本审核流程未走完
            if (InputParamsValidator.isDuplicateExist(entity.getDuplicateVersion())) {
                //判断板块是否已提交流程
                taskInfo = AuditUtil.getTaskByIdAndVersionAndType(entity.getId(), entity.getDuplicateVersion(), ResourceTypeEnum.TAB.getValue());
                if (taskInfo != null && taskInfo.getTaskFlag() == 1) {
                    return "[ID：" + entity.getId() + "][" + commonLanguageProperties.getName() + "：" + entity.getTabName() + "]this panel [V" + entity.getDuplicateVersion() + "] copies exist in the audit task flow " + commonLanguageProperties.getNotDelete();

                }
            }
            thisBizImpl.updateFlag(id, userId, ResourceStatusEnums.OFF_LINE.getStatus());
            //删除关联信息
            this.handleLogicDelRls(entity, id, entity.getCurrentVersion());
        }
        return R.SUCCESS;
    }

    @Override
    public void deleteHistory(Integer id, Integer version, Integer userId) {
        TabInfoEntity entity = (TabInfoEntity) thisBizImpl.queryById(id);
        Assert.notNull(entity, "删除的版面不存在，版面ID： " + id);
        if (!BaseAuditTaskProviderImpl.V_1.equals(version) && entity.getCurrentVersion().equals(version)) {
            //如果不是V1版本，而且和当前版本一样
            throw new AdminBizException(commonLanguageProperties.getCopy() + commonLanguageProperties.getSubmitReview() + commonLanguageProperties.getNotDelete());
        }
        //处理v1待审核或者草稿版本的删除
        this.handleDelHistoryForV1(entity);
        //更新副本删除状态
        thisBizImpl.updateHistoryFlag(id, version, userId, ResourceStatusEnums.OFF_LINE.getStatus());
        if (BaseAuditTaskProviderImpl.V_1.equals(version)) {
            //V1版本的处理
            thisBizImpl.updateFlag(id, userId, ResourceStatusEnums.OFF_LINE.getStatus());
        } else {
            //副本的处理
            TabInfoEntity tabInfo = new TabInfoEntity();
            tabInfo.setId(id);
            //置空副本标识
            tabInfo.setDuplicateVersion(null);
            tabInfo.setModifier(userId);
            thisBizImpl.updateById(tabInfo);
        }
        //删除关联信息
        this.handleLogicDelRls(entity, id, version);

    }

    @Override
    public void handleCurrentEditAndCopyEdit(Integer autoIncrementId, TabInfoEntity inputEntity, Integer userId) {
        super.handleCurrentEditAndCopyEdit(autoIncrementId, inputEntity, userId);
        //删除关联信息
        thisRlsUpdateProvider.deleteAllRls(autoIncrementId, inputEntity.getCurrentVersion());
    }

    /*************** ===私有方法=== ********************* ===start=== ***************************************/
    /**
     * 处理v1待审核或者草稿版本的删除
     *
     * @param entity
     */
    private void handleDelHistoryForV1(TabInfoEntity entity) {
        if (BaseAuditTaskProviderImpl.V_1.equals(entity.getCurrentVersion()) && ResourceStatusValidator.isToAuditingOrDraft(entity.getStatus())) {
            //已经被使用不能删除
            if (thisBizImpl.isTabHasUsed(entity.getId())) {
                throw new AdminBizException("[ID：" + entity.getId() + "][" + commonLanguageProperties.getName() + "：" + entity.getTabName() + "]" + commonLanguageProperties.getInUse() + commonLanguageProperties.getNotDelete());
            }
        }
    }

    /**
     * 删除关联信息
     *
     * @param entity
     * @param id
     * @param version
     */
    private void handleLogicDelRls(TabInfoEntity entity, Integer id, Integer version) {
        if (TabTypeConstants.isNormal(entity.getTabType())) {
            tabPanelRlsBiz.updateTabPanelRlsFlag(id, version, ResourceStatusEnums.OFF_LINE.getStatus());
        } else if (TabTypeConstants.isPoster(entity.getTabType())) {
            //似乎没必要
        }
        DmpRegistryRlsUtil.deleteDmpRls(id, ResourceTypeEnum.TAB.getValue(), version);
    }

}
